import java.awt.BorderLayout;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JList;
import javax.swing.JSpinner;

import java.awt.Font;

import javax.swing.JRadioButton;
import javax.swing.JSlider;
import javax.swing.JToggleButton;

import java.awt.Color;

import javax.swing.border.BevelBorder;
import javax.swing.border.LineBorder;
import javax.swing.JTextPane;
import javax.swing.JLabel;

import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.ChangeEvent;
import javax.swing.SwingConstants;
import javax.swing.JScrollBar;
import javax.swing.JSeparator;
import javax.swing.JTabbedPane;
import javax.swing.JComboBox;


public class Gui_main extends JFrame {

	 Logic logic = null;
	 
	 JPanel contentPane;
	 int[] capac_calib_min = new int[6];
	 int[] capac_calib_max = {1024,1024,1024,1024,1024,1024};//new int[6];
	 
	 boolean robot_standing = false;

	 
	 
		
	String ss="";
	
	JTextArea textArea = new JTextArea();
	JTextArea textArea_runtime = new JTextArea();
	
	//servo
	JTextField textField_servonum;
	JTextField textField_servo_speed;
	JSlider slider_servovalue;
	
	//leg AD
	JTextField textField_leg_1;
	JTextField textField_leg_2;
	JTextField textField_leg_3;
	JTextField textField_leg_6;
	JTextField textField_leg_5;
	JTextField textField_leg_4;
	
	//supply
	JPanel panel_servo_supply_color_indicator;
	
	//battery
	JTextField textField_battery;
	
	JTextArea textArea_leg_commands;
	JComboBox comboBox_filelist;
	JTextField textField_servopos;
	



	 
	public Color Int_to_GreenYellowRed(int value) {
		// 0-33: green
		// 34-66: yellow
		// 67-100: red
		if(value>100)return (new Color(0,0,0));		// dummy input -> black color :)		
		if(value<50)return (new Color(155+value*2, 255, 0));//green
		if(value>50)return (new Color(255,  (255-((value-50)*3)),   0));//red
		return (new Color(255, 255, 0));//yellow
	}




	public Gui_main(final Logic logicparameter) {
		
		logic = logicparameter;

		setResizable(false);
		setTitle("HEXAPOD");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(0, 0, 1024, 600);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		
		JButton btnSTOP = new JButton("STOP");
		btnSTOP.setBounds(897, 413, 103, 159);
		btnSTOP.setBackground(Color.RED);
		btnSTOP.setFont(new Font("Tahoma", Font.BOLD, 20));
		btnSTOP.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				logic.Send_Stop();
			}
		});
		contentPane.setLayout(null);
		contentPane.add(btnSTOP);
		
		JPanel panel_textfield = new JPanel();
		panel_textfield.setBounds(0, 405, 513, 167);
		contentPane.add(panel_textfield);
		panel_textfield.setLayout(null);	
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(0, 0, 510, 144);
		panel_textfield.add(scrollPane);
		
		textArea.setFont(new Font("Monospaced", Font.PLAIN, 12));
		scrollPane.setViewportView(textArea);
		
		JButton btnTxtClear = new JButton("Clear");
		btnTxtClear.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				textArea.setText("");
			}
		});
		btnTxtClear.setBounds(421, 144, 89, 23);
		panel_textfield.add(btnTxtClear);
		
		JPanel panel_Serial = new JPanel();
		panel_Serial.setBounds(0, 0, 173, 69);
		panel_Serial.setBorder(new LineBorder(new Color(0, 0, 0)));
		contentPane.add(panel_Serial);
		panel_Serial.setLayout(null);
		
		JButton btnConnect = new JButton("Connect");
		btnConnect.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				//logic.connect();
				//logic.misep.serialport.connect("Serialoptions.txt");
				System.out.println("Connect button pressed");
								
			}
		});
		btnConnect.setBounds(10, 11, 109, 23);
		panel_Serial.add(btnConnect);
		
		JRadioButton rdbtnAutoconnect = new JRadioButton("Autoconnect");
		rdbtnAutoconnect.setBounds(10, 41, 109, 23);
		panel_Serial.add(rdbtnAutoconnect);
		
		JPanel panel_Conn_status = new JPanel();
		panel_Conn_status.setBackground(logic.getconnectionsatatus());
		panel_Conn_status.setBounds(132, 11, 31, 32);
		panel_Serial.add(panel_Conn_status);
		
		JPanel panel_runtime = new JPanel();
		panel_runtime.setBounds(10, 80, 95, 69);
		contentPane.add(panel_runtime);
		panel_runtime.setLayout(null);
		
		JScrollPane scrollPane_runtime = new JScrollPane();
		scrollPane_runtime.setBounds(10, 21, 65, 30);
		panel_runtime.add(scrollPane_runtime);
		
		scrollPane_runtime.setViewportView(textArea_runtime);
		
		JPanel panel_servotest = new JPanel();
		panel_servotest.setBounds(517, 425, 370, 114);
		panel_servotest.setBorder(new LineBorder(new Color(0, 0, 0)));
		contentPane.add(panel_servotest);
		panel_servotest.setLayout(null);
		
		textField_servonum = new JTextField();
		textField_servonum.setText("0");
		textField_servonum.setBounds(10, 48, 86, 20);
		panel_servotest.add(textField_servonum);
		textField_servonum.setColumns(10);
		
		JLabel lblServoNumber = new JLabel("Servo number [ 1 - 18 ]");
		lblServoNumber.setBounds(10, 11, 129, 14);
		panel_servotest.add(lblServoNumber);
		
		JLabel lblSpeedDelayBetween = new JLabel("Delay between steeps");
		lblSpeedDelayBetween.setBounds(245, 11, 115, 14);
		panel_servotest.add(lblSpeedDelayBetween);
		
		textField_servo_speed = new JTextField();
		textField_servo_speed.setText("100");
		textField_servo_speed.setBounds(274, 48, 86, 20);
		panel_servotest.add(textField_servo_speed);
		textField_servo_speed.setColumns(10);
		
		slider_servovalue = new JSlider();
		slider_servovalue.setMinimum(0);
		slider_servovalue.setMaximum(200);
		slider_servovalue.setValue(100);
		slider_servovalue.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent e) {
				logic.Send_servo_position(Integer.parseInt(textField_servonum.getText()), slider_servovalue.getValue() ,  Integer.parseInt(textField_servo_speed.getText()) );
				
				//System.out.println( Integer.parseInt(textField_servonum.getText()));
				//System.out.println( slider_servovalue.getValue() );
				//System.out.println( Integer.parseInt(textField_servo_speed.getText()));
				textField_servopos.setText( Integer.toString(slider_servovalue.getValue()) );
				
			}
		});
		slider_servovalue.setBounds(20, 80, 340, 23);
		panel_servotest.add(slider_servovalue);
		
		textField_servopos = new JTextField();
		textField_servopos.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				logic.Send_servo_position(Integer.parseInt(textField_servonum.getText()), Integer.parseInt(textField_servopos.getText()) ,  Integer.parseInt(textField_servo_speed.getText()) );
			}
		});
		textField_servopos.setBounds(144, 48, 86, 20);
		panel_servotest.add(textField_servopos);
		textField_servopos.setColumns(10);
		
		JLabel lblPos = new JLabel("Position");
		lblPos.setBounds(149, 11, 74, 14);
		panel_servotest.add(lblPos);
		
		JPanel panel_leg_AD = new JPanel();
		panel_leg_AD.setBounds(736, 25, 272, 167);
		panel_leg_AD.setBorder(new LineBorder(new Color(0, 0, 0)));
		contentPane.add(panel_leg_AD);
		panel_leg_AD.setLayout(null);
		
		JPanel panel = new JPanel();
		panel.setBackground(Color.GRAY);
		panel.setBounds(99, 11, 79, 145);
		panel_leg_AD.add(panel);
		
		textField_leg_1 = new JTextField();
		textField_leg_1.setBackground(Color.GREEN);
		textField_leg_1.setBounds(178, 11, 86, 20);
		panel_leg_AD.add(textField_leg_1);
		textField_leg_1.setColumns(10);
		
		textField_leg_2 = new JTextField();
		textField_leg_2.setBackground(Color.GREEN);
		textField_leg_2.setColumns(10);
		textField_leg_2.setBounds(178, 72, 86, 20);
		panel_leg_AD.add(textField_leg_2);
		
		textField_leg_3 = new JTextField();
		textField_leg_3.setBackground(Color.GREEN);
		textField_leg_3.setColumns(10);
		textField_leg_3.setBounds(178, 136, 86, 20);
		panel_leg_AD.add(textField_leg_3);
		
		textField_leg_6 = new JTextField();
		textField_leg_6.setBackground(Color.GREEN);
		textField_leg_6.setColumns(10);
		textField_leg_6.setBounds(10, 11, 86, 20);
		panel_leg_AD.add(textField_leg_6);
		
		textField_leg_5 = new JTextField();
		textField_leg_5.setBackground(Color.GREEN);
		textField_leg_5.setColumns(10);
		textField_leg_5.setBounds(10, 72, 86, 20);
		panel_leg_AD.add(textField_leg_5);
		
		textField_leg_4 = new JTextField();
		textField_leg_4.setBackground(Color.GREEN);
		textField_leg_4.setColumns(10);
		textField_leg_4.setBounds(10, 136, 86, 20);
		panel_leg_AD.add(textField_leg_4);
		
		JPanel panel_supply_servo = new JPanel();
		panel_supply_servo.setBounds(889, 203, 111, 101);
		panel_supply_servo.setBorder(new LineBorder(new Color(0, 0, 0)));
		contentPane.add(panel_supply_servo);
		panel_supply_servo.setLayout(null);
		
		panel_servo_supply_color_indicator = new JPanel();
		panel_servo_supply_color_indicator.setBackground(Color.RED);
		panel_servo_supply_color_indicator.setBounds(10, 11, 89, 30);
		panel_supply_servo.add(panel_servo_supply_color_indicator);
		
		JButton btn_supply_ON = new JButton("ON");
		btn_supply_ON.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				logic.Send_supply_ONOFF(1);
			}
		});
		btn_supply_ON.setBounds(10, 52, 89, 23);
		panel_supply_servo.add(btn_supply_ON);
		
		JButton btn_supply_OFF = new JButton("OFF");
		btn_supply_OFF.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				logic.Send_supply_ONOFF(0);
			}
		});
		btn_supply_OFF.setBounds(10, 76, 89, 23);
		panel_supply_servo.add(btn_supply_OFF);
		
		JPanel panel_leg_move = new JPanel();
		panel_leg_move.setBorder(new LineBorder(new Color(0, 0, 0)));
		panel_leg_move.setBounds(180, 0, 546, 394);
		contentPane.add(panel_leg_move);
		panel_leg_move.setLayout(null);
		
		comboBox_filelist = new JComboBox();

		String[] filelist = logic.move_editor.Get_txt_file_list("E:\\ javaguicodes\\Hexapod\\LEG_MOVEMENTS");
		if(filelist != null){
			 for (int i=0; i<filelist.length; i++)
			 {
				 String filename = filelist[i];
				 comboBox_filelist.addItem(filename);
			 }
		}

		comboBox_filelist.setBounds(413, 11, 123, 20);
		panel_leg_move.add(comboBox_filelist);
		
		JPanel panel_leg_commands = new JPanel();
		panel_leg_commands.setBounds(10, 11, 378, 372);
		panel_leg_move.add(panel_leg_commands);
		panel_leg_commands.setLayout(null);
		
		JScrollPane scrollPane_leg_commands = new JScrollPane();
		scrollPane_leg_commands.setBounds(10, 11, 359, 350);
		panel_leg_commands.add(scrollPane_leg_commands);
		
		textArea_leg_commands = new JTextArea();
		scrollPane_leg_commands.setViewportView(textArea_leg_commands);
		
		JButton btn_open = new JButton("OPEN");
		btn_open.setBounds(447, 42, 89, 23);
		panel_leg_move.add(btn_open);
		btn_open.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				int selected_item_index = comboBox_filelist.getSelectedIndex();
				String[] filelist = logic.move_editor.Get_txt_file_list("E:\\ javaguicodes\\Hexapod\\LEG_MOVEMENTS");
				String dir = ("E:\\ javaguicodes\\Hexapod\\LEG_MOVEMENTS" + "\\" + filelist[selected_item_index]);
				
				String data = logic.move_editor.Read_file_to_string( dir );
				
				
				textArea_leg_commands.setText(data);
			}
		});
		
		JButton btn_write_txt = new JButton("Write");
		btn_write_txt.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				int selected_item_index = comboBox_filelist.getSelectedIndex();
				String[] filelist = logic.move_editor.Get_txt_file_list("E:\\ javaguicodes\\Hexapod\\LEG_MOVEMENTS");
				String dir = ("E:\\ javaguicodes\\Hexapod\\LEG_MOVEMENTS" + "\\" + filelist[selected_item_index]);
				
				logic.move_editor.Write_string_to_file(dir, logic.move_editor.String_ultimate_hack_for_endline(textArea_leg_commands.getText()));
			}
		});
		btn_write_txt.setBounds(447, 97, 89, 23);
		panel_leg_move.add(btn_write_txt);
		
		JButton btn_execute = new JButton("EXECUTE");
		btn_execute.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				logic.move_editor.Execute_movement(logic.move_editor.String_ultimate_hack_for_endline(textArea_leg_commands.getText()));
			}
		});
		btn_execute.setBounds(447, 215, 89, 41);
		panel_leg_move.add(btn_execute);
		
		JButton btn_zero = new JButton("ZERO");
		btn_zero.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				logic.Send_servo_position(1, 100, 1);
				logic.Send_servo_position(2, 200, 1);
				logic.Send_servo_position(3, 100, 1);
				logic.Send_servo_position(4, 100, 1);
				logic.Send_servo_position(5, 200, 1);
				logic.Send_servo_position(6, 100, 1);
				logic.Send_servo_position(7, 100, 1);
				logic.Send_servo_position(8, 200, 1);				
				logic.Send_servo_position(9, 100, 1);
				logic.Send_servo_position(10, 100, 1);
				logic.Send_servo_position(11, 200, 1);
				logic.Send_servo_position(12, 100, 1);
				logic.Send_servo_position(13, 100, 1);
				logic.Send_servo_position(14, 200, 1);
				logic.Send_servo_position(15, 100, 1);
				logic.Send_servo_position(16, 100, 1);
				logic.Send_servo_position(17, 200, 1);
				logic.Send_servo_position(18, 100, 1);
			}
		});
		btn_zero.setBounds(447, 157, 89, 23);
		panel_leg_move.add(btn_zero);
		

		
		JPanel panel_battery = new JPanel();
		panel_battery.setBorder(new LineBorder(new Color(0, 0, 0)));
		panel_battery.setBounds(736, 203, 117, 62);
		contentPane.add(panel_battery);
		panel_battery.setLayout(null);
		
		textField_battery = new JTextField();
		textField_battery.setBounds(10, 29, 86, 20);
		panel_battery.add(textField_battery);
		textField_battery.setColumns(10);
		
		JLabel lbl_battery = new JLabel("Battery voltage");
		lbl_battery.setBounds(10, 11, 86, 14);
		panel_battery.add(lbl_battery);

		
		
		Start_GUI_Thread();
				
	}


		
	
	
	public void Start_GUI_Thread() {
	    new Thread(new GUI_Thread()).start();
	}
	
	 private class GUI_Thread implements Runnable {
	    public void run() {
	        while(true) {
	            try {
	                Thread.sleep(200);
	            } catch(InterruptedException ex) {
	                Thread.currentThread().interrupt();
	            }
	           /** do things here*/
	            
	            if(logic.move_editor.robot_moving_now == false){
	            	if(robot_standing == false && logic.RC_controller[0]>150){
	            		System.out.println("FELKEL");
	            		robot_standing= true;
	            		logic.move_editor.Execute_movement(logic.move_editor.String_ultimate_hack_for_endline(logic.move_editor.Read_file_to_string(("E:\\ javaguicodes\\Hexapod\\LEG_MOVEMENTS" + "\\" + "felkel.txt") )));
	            	}
	            	
	             	if(robot_standing == true && logic.RC_controller[0]<50){
	            		System.out.println("LEFEKSZIK");
	            		robot_standing= false;
	            		logic.move_editor.Execute_movement(logic.move_editor.String_ultimate_hack_for_endline(logic.move_editor.Read_file_to_string(("E:\\ javaguicodes\\Hexapod\\LEG_MOVEMENTS" + "\\" + "Alaphelyzet.txt") )));
	            	}
	             	
	              	if(robot_standing == true && logic.RC_controller[1]>150){
	            		System.out.println("fordul:J");
	            		logic.move_editor.Execute_movement(logic.move_editor.String_ultimate_hack_for_endline(logic.move_editor.Read_file_to_string(("E:\\ javaguicodes\\Hexapod\\LEG_MOVEMENTS" + "\\" + "forog_jobbra.txt") )));
	            		
	            	}
	              	
	              	if(robot_standing == true && logic.RC_controller[1]<50){
	            		System.out.println("fordul:B");
	            		logic.move_editor.Execute_movement(logic.move_editor.String_ultimate_hack_for_endline(logic.move_editor.Read_file_to_string(("E:\\ javaguicodes\\Hexapod\\LEG_MOVEMENTS" + "\\" + "forog_balra.txt") )));
	            		
	            	}
	              	
	              	if(robot_standing == true && logic.RC_controller[2]>150){
	              		System.out.println("elore");
	              		logic.move_editor.Execute_movement(logic.move_editor.String_ultimate_hack_for_endline(logic.move_editor.Read_file_to_string(("E:\\ javaguicodes\\Hexapod\\LEG_MOVEMENTS" + "\\" + "mozog_elore.txt") )));
	            	}
	              	
	              	if(robot_standing == true && logic.RC_controller[2]<50){
	            		System.out.println("hatra");
	            		logic.move_editor.Execute_movement(logic.move_editor.String_ultimate_hack_for_endline(logic.move_editor.Read_file_to_string(("E:\\ javaguicodes\\Hexapod\\LEG_MOVEMENTS" + "\\" + "mozog_hatra.txt") )));
	            		
	            	}
	              	
	              	if(robot_standing == true && logic.RC_controller[3]>150){
	            		System.out.println("jobbra");
	            		logic.move_editor.Execute_movement(logic.move_editor.String_ultimate_hack_for_endline(logic.move_editor.Read_file_to_string(("E:\\ javaguicodes\\Hexapod\\LEG_MOVEMENTS" + "\\" + "mozog_jobbra.txt") )));
	            		
	            	}
	              	
	              	if(robot_standing == true && logic.RC_controller[3]<50){
	            		System.out.println("balra");
	            		logic.move_editor.Execute_movement(logic.move_editor.String_ultimate_hack_for_endline(logic.move_editor.Read_file_to_string(("E:\\ javaguicodes\\Hexapod\\LEG_MOVEMENTS" + "\\" + "mozog_balra.txt") )));
	            		
	            	}
	            }

	            /** refresh the fields*/
				// Text box
				if (logic.isnewtext) {
					logic.stringlock.lock();
					textArea.append(logic.textmsg);
					textArea.setCaretPosition(textArea.getDocument().getLength());
					logic.textmsg = "";
					logic.isnewtext = false;
					logic.stringlock.unlock();

				}

				// Task %
				textArea_runtime.setText("");
				textArea_runtime.append(logic.runtime);

	 
				//TODO

				
				//LEG CURRENT
							
				textField_leg_1.setText(Integer.toString(logic.Leg_currents[0]) + "mA");
				textField_leg_1.setBackground(Int_to_GreenYellowRed( logic.Leg_color[0] ));
				
				textField_leg_2.setText(Integer.toString(logic.Leg_currents[1]) + "mA");
				textField_leg_2.setBackground(Int_to_GreenYellowRed( logic.Leg_color[1] ));
				
				textField_leg_3.setText(Integer.toString(logic.Leg_currents[2]) + "mA");
				textField_leg_3.setBackground(Int_to_GreenYellowRed( logic.Leg_color[2] ));
				
				textField_leg_4.setText(Integer.toString(logic.Leg_currents[3]) + "mA");
				textField_leg_4.setBackground(Int_to_GreenYellowRed( logic.Leg_color[3] ));
				
				textField_leg_5.setText(Integer.toString(logic.Leg_currents[4]) + "mA");
				textField_leg_5.setBackground(Int_to_GreenYellowRed( logic.Leg_color[4] ));
				
				textField_leg_6.setText(Integer.toString(logic.Leg_currents[5]) + "mA");
				textField_leg_6.setBackground(Int_to_GreenYellowRed( logic.Leg_color[5] ));
				
				//supply
				if(logic.supply_servo_state==1)
					panel_servo_supply_color_indicator.setBackground(Color.GREEN);
				else
					panel_servo_supply_color_indicator.setBackground(Color.RED);
				
				//batt voltage
				textField_battery.setText(Integer.toString(logic.battery_voltage / 100) + "." + Integer.toString(logic.battery_voltage % 100) + "V");
				
				
		
				
				
	           
	          //till this line  
	        }
	    }
	}
}// end of gui
